package com.anytron.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.anytron.model.NodeInfo;
import com.anytron.model.req.PageRequest;
import com.anytron.model.res.ResultData;
import com.anytron.service.NodeInfoService;
import com.github.pagehelper.PageInfo;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.DES;
import tk.mybatis.mapper.entity.Example;


@RestController
@RequestMapping(value = "node")
public class NodeController {
	
	@Value("${des.key}")
	private String key;
	
	@Autowired
	NodeInfoService nodeInfoService;
	
	@RequestMapping(value = "list")
	public ResultData<PageInfo<NodeInfo>> list(PageRequest pageRequest,NodeInfo nodeInfo) throws Exception {
		ResultData<PageInfo<NodeInfo>> resultData = new ResultData<>();
		Example example=new Example(NodeInfo.class);
	    Example.Criteria criteria=example.createCriteria();
	    example.setOrderByClause("node_name asc");
	    if (StringUtils.isNotBlank(nodeInfo.getNodeBName())) {
	    	criteria.andLike("nodeBName", "%"+nodeInfo.getNodeBName()+"%");
		}
	    if (StringUtils.isNotBlank(nodeInfo.getNodeHost())) {
	    	criteria.andLike("nodeHost", "%"+nodeInfo.getNodeHost()+"%");
	    }
		PageInfo<NodeInfo> pageInfo = nodeInfoService.pageByExample(pageRequest, example);
		//密码清空
		pageInfo.getList().forEach(item->item.setNodePasw(null));
		resultData.setData(pageInfo);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "queryByNodeInfo")
	public ResultData<List<NodeInfo>> queryByNodeInfo(@RequestBody NodeInfo nodeInfo) throws Exception {
		ResultData<List<NodeInfo>> resultData = new ResultData<>();
		List<NodeInfo> list = nodeInfoService.list(nodeInfo);
		resultData.setData(list);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "add" ,method = RequestMethod.POST)
	public ResultData<String> add(@RequestBody NodeInfo nodeInfo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		//加密密码
		String nodePasw = nodeInfo.getNodePasw();
		DES des = new DES(Mode.ECB, Padding.PKCS5Padding, key.getBytes());
		nodeInfo.setNodePasw(des.encryptBase64(nodePasw));
		nodeInfo.setNodePin(nodeInfo.getNodeHost()+nodeInfo.getNodePort()+nodeInfo.getNodeUser());
		int res = nodeInfoService.add(nodeInfo);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("节点信息保存失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "update")
	public ResultData<String> update(@RequestBody NodeInfo nodeInfo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		if (StringUtils.isNoneBlank(nodeInfo.getNodePasw())) {
			//加密密码
			String nodePasw = nodeInfo.getNodePasw();
			DES des = new DES(Mode.ECB, Padding.PKCS5Padding, key.getBytes());
			nodeInfo.setNodePasw(des.encryptBase64(nodePasw));
		}
		nodeInfo.setNodePin(nodeInfo.getNodeHost()+nodeInfo.getNodePort()+nodeInfo.getNodeUser());
		int res = nodeInfoService.update(nodeInfo);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("节点信息更新失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "delete")
	public ResultData<String> delete(@RequestBody NodeInfo nodeInfo) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		int res = nodeInfoService.delete(nodeInfo.getId());
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("节点信息删除失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
}
